package com.ruoyi.receipt.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.receipt.domain.TbUser;
import com.ruoyi.receipt.mapper.TbStatisAnalysisMapper;
import com.ruoyi.receipt.mapper.TbUserMapper;
import com.ruoyi.receipt.utils.UserInitUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.ruoyi.receipt.mapper.TbRecordOnlineUserMapper;
import com.ruoyi.receipt.domain.TbRecordOnlineUser;
import com.ruoyi.receipt.service.ITbRecordOnlineUserService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 记录在线用户Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-02
 */
@Service
public class TbRecordOnlineUserServiceImpl implements ITbRecordOnlineUserService 
{
    @Autowired
    private TbRecordOnlineUserMapper tbRecordOnlineUserMapper;

    @Autowired
    private TbStatisAnalysisMapper tbStatisAnalysisMapper;

    @Autowired
    IdWorker idWorker;

    @Autowired
    TbUserMapper tbUserMapper;

    /**
     * 查询记录在线用户
     * 
     * @param id 记录在线用户ID
     * @return 记录在线用户
     */
    @Override
    public TbRecordOnlineUser selectTbRecordOnlineUserById(Long id)
    {
        return tbRecordOnlineUserMapper.selectTbRecordOnlineUserById(id);
    }

    /**
     * 查询记录在线用户列表
     * 
     * @param tbRecordOnlineUser 记录在线用户
     * @return 记录在线用户
     */
    @Override
    public List<TbRecordOnlineUser> selectTbRecordOnlineUserList(TbRecordOnlineUser tbRecordOnlineUser)
    {
        return tbRecordOnlineUserMapper.selectTbRecordOnlineUserList(tbRecordOnlineUser);
    }

    @Override
    public List<TbRecordOnlineUser> selectYesterdayOnlineUser() {
        return tbRecordOnlineUserMapper.selectYesterdayOnlineUser();
    }

    @Override
    public List<TbRecordOnlineUser> selectTodayOnlineUser() {
        return tbRecordOnlineUserMapper.selectTodayOnlineUser();
    }

    /**
     * 新增记录在线用户
     * 
     * @param tbRecordOnlineUser 记录在线用户
     * @return 结果
     */
    @Override
    public int insertTbRecordOnlineUser(TbRecordOnlineUser tbRecordOnlineUser)
    {
        tbRecordOnlineUser.setCreateTime(DateUtils.getNowDate());
        return tbRecordOnlineUserMapper.insertTbRecordOnlineUser(tbRecordOnlineUser);
    }

    /**
     * 修改记录在线用户
     * 
     * @param tbRecordOnlineUser 记录在线用户
     * @return 结果
     */
    @Override
    public int updateTbRecordOnlineUser(TbRecordOnlineUser tbRecordOnlineUser)
    {
        tbRecordOnlineUser.setUpdateTime(DateUtils.getNowDate());
        UserInitUtils.initUserInfoForUpdate(tbRecordOnlineUser);
        return tbRecordOnlineUserMapper.updateTbRecordOnlineUser(tbRecordOnlineUser);
    }

    /**
     * 批量删除记录在线用户
     * 
     * @param ids 需要删除的记录在线用户ID
     * @return 结果
     */
    @Override
    public int deleteTbRecordOnlineUserByIds(Long[] ids)
    {
        return tbRecordOnlineUserMapper.deleteTbRecordOnlineUserByIds(ids);
    }

    /**
     * 删除记录在线用户信息
     * 
     * @param id 记录在线用户ID
     * @return 结果
     */
    @Override
    public int deleteTbRecordOnlineUserById(Long id)
    {
        return tbRecordOnlineUserMapper.deleteTbRecordOnlineUserById(id);
    }

    @Override
    @Transactional
    public int insertOnlineUserYestody() {
        //先去查询tb_record_online_user表中是否已经插入昨天登录的用户信息 有的先删除再插入
        int i = tbRecordOnlineUserMapper.deleteOnlineUserYestody();
        //先去查询tb_statis_analysis表中是否已经插入昨天的统计分析数据 有的先删除再插入
        int i1 = tbStatisAnalysisMapper.deleteTbStatisAnalysisYestody();
        //先去查询昨日登录的用户
        List<TbUser> tbUsers = tbUserMapper.selectTbUserListYestody();
        int i3 = 0;
        for (int i2 = 0; i2 < tbUsers.size(); i2++) {
            TbRecordOnlineUser tb = new TbRecordOnlineUser();
            tb.setId(idWorker.nextId());
            tb.setUserId(tbUsers.get(i2).getId());
            tb.setRealName(tbUsers.get(i2).getRealName());
            tb.setType(tbUsers.get(i2).getType());
            tb.setLastLoginTime(tbUsers.get(i2).getLastLoginTime());
            tb.setLoginAccount(tbUsers.get(i2).getLoginAccount());
            tb.setIsDel(0);
            i3 = tbRecordOnlineUserMapper.insertTbRecordOnlineUser(tb);
        }
        return i3;
    }

    @Override
    @Transactional
    public int insertOnlineUserTody() {
        //先去查询tb_record_online_user表中是否已经插入今天登录的用户信息 有的先删除再插入
        int i = tbRecordOnlineUserMapper.deleteOnlineUserTody();
        //先去查询tb_statis_analysis表中是否已经插入今天的统计分析数据 有的先删除再插入
        int i1 = tbStatisAnalysisMapper.deleteTbStatisAnalysisTody();
        //先去查询今日登录的用户
        List<TbUser> tbUsers = tbUserMapper.selectTbUserListLoginTody();
        int i3 = 0;
        for (int i2 = 0; i2 < tbUsers.size(); i2++) {
            TbRecordOnlineUser tb = new TbRecordOnlineUser();
            tb.setId(idWorker.nextId());
            tb.setUserId(tbUsers.get(i2).getId());
            tb.setRealName(tbUsers.get(i2).getRealName());
            tb.setType(tbUsers.get(i2).getType());
            tb.setLastLoginTime(tbUsers.get(i2).getLastLoginTime());
            tb.setLoginAccount(tbUsers.get(i2).getLoginAccount());
            tb.setIsDel(0);
            i3 = tbRecordOnlineUserMapper.insertTbRecordOnlineUser(tb);
        }
        return i3;
    }
}
